// Copyright by Thomas Maierhofer / MSE-iT, Hauptstrasse 32, 88427 Bad Schussenried, Germany
// See licence file of project for licencing details
// Contact: thomas@maierhofer.de

// ***********************************************
// This is the only header file to include 
// ***********************************************

#pragma once

// Agressive Optimization inspired from http://www.codeproject.com/KB/tips/aggressiveoptimize.aspx

#pragma warning(disable:4711)
#ifdef NDEBUG
// /Og (global optimizations), /Os (favor small code), /Oy (no frame pointers)
#pragma optimize("gsy",on)
#pragma comment(linker,"/merge:.text=.data")
#endif // NDEBUG

enum LibMsgEnum
{
	LIB_DIALOG_TITLE,
	LIB_DIALOG_BUTTON_BACK,
	LIB_DIALOG_BUTTON_NEXT,
	LIB_DIALOG_BUTTON_CANCEL,
	LIB_DIALOG_BUTTON_OK,
	LIB_LAST // This is the last (unused) message index for the library, start the app messages here.
};



#pragma region OleRichEditCtrl

class COleRichEditCtrl : public CRichEditCtrl
{
// Construction
public:
	COleRichEditCtrl();
	virtual ~COleRichEditCtrl();

	long StreamInFromResource(int iRes, LPCTSTR sType);
	void SetRtfText(CString text );



protected:
	static DWORD CALLBACK readFunction(DWORD dwCookie,
		 LPBYTE lpBuf,			// the buffer to fill
		 LONG nCount,			// number of bytes to read
		 LONG* nRead);			// number of bytes actually read

	interface IExRichEditOleCallback;	// forward declaration (see below in this header file)

	IExRichEditOleCallback* m_pIRichEditOleCallback;
	BOOL m_bCallbackSet;
	
	
	interface IExRichEditOleCallback : public IRichEditOleCallback
	{
	public:


		IExRichEditOleCallback();
		virtual ~IExRichEditOleCallback();
		int m_iNumStorages;
		IStorage* pStorage;
		DWORD m_dwRef;

		virtual HRESULT STDMETHODCALLTYPE GetNewStorage(LPSTORAGE* lplpstg);
		virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void ** ppvObject);
		virtual ULONG STDMETHODCALLTYPE AddRef();
		virtual ULONG STDMETHODCALLTYPE Release();
		virtual HRESULT STDMETHODCALLTYPE GetInPlaceContext(LPOLEINPLACEFRAME FAR *lplpFrame,
			LPOLEINPLACEUIWINDOW FAR *lplpDoc, LPOLEINPLACEFRAMEINFO lpFrameInfo);
 		virtual HRESULT STDMETHODCALLTYPE ShowContainerUI(BOOL fShow);
 		virtual HRESULT STDMETHODCALLTYPE QueryInsertObject(LPCLSID lpclsid, LPSTORAGE lpstg, LONG cp);
 		virtual HRESULT STDMETHODCALLTYPE DeleteObject(LPOLEOBJECT lpoleobj);
 		virtual HRESULT STDMETHODCALLTYPE QueryAcceptData(LPDATAOBJECT lpdataobj, CLIPFORMAT FAR *lpcfFormat,
			DWORD reco, BOOL fReally, HGLOBAL hMetaPict);
 		virtual HRESULT STDMETHODCALLTYPE ContextSensitiveHelp(BOOL fEnterMode);
 		virtual HRESULT STDMETHODCALLTYPE GetClipboardData(CHARRANGE FAR *lpchrg, DWORD reco, LPDATAOBJECT FAR *lplpdataobj);
 		virtual HRESULT STDMETHODCALLTYPE GetDragDropEffect(BOOL fDrag, DWORD grfKeyState, LPDWORD pdwEffect);
 		virtual HRESULT STDMETHODCALLTYPE GetContextMenu(WORD seltyp, LPOLEOBJECT lpoleobj, CHARRANGE FAR *lpchrg,
			HMENU FAR *lphmenu);
	};
 

public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(COleRichEditCtrl)
	protected:
	virtual void PreSubclassWindow();
	//}}AFX_VIRTUAL

// Implementation
public:


	// Generated message map functions
protected:
	//{{AFX_MSG(COleRichEditCtrl)
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	//}}AFX_MSG

	DECLARE_MESSAGE_MAP()
};


#pragma endregion




#pragma region Wizard
// **************************************************************************
class CWizardPage;
class CWizardDialog : public CDialog
{
public:
	friend class CWizardPage;
	CWizardDialog (LPCTSTR lpszTemplate, CWnd* pParent = NULL); // Create with new, memory is freed on exit;
	CWizardDialog (UINT nIDTemplate, CWnd* pParent = NULL); // Create with new, memory is freed on exit;
	virtual ~CWizardDialog ();


protected:
	CObList m_PageList;		// this list of CWizardPage objects
	UINT m_nPlaceholderID;	// control ID for the palceholder

	HBITMAP backgroundLocalized;
	HBITMAP background;		
	HBITMAP defaultBackgroundLocalized;	
	HBITMAP defaultBackground;

public:
	void AddPage(CWizardPage* pPage, UINT nID);

	BOOL SetFirstPage();
	void SetNextPage();

	void EnableFinish(BOOL bEnable);
	void EnableBack(BOOL bEnable);
	void EnableNext(BOOL bEnable);
	void EnableCancel( BOOL bEnable);

	int GetActiveIndex() const;
	int GetPageIndex(CWizardPage* pPage) const;
	int GetPageCount();
	CWizardPage* GetPage(int nPage) const;
	CWizardPage* GetPageByID(UINT nResourceID);


	BOOL SetActivePage(int nPage);
	BOOL SetActivePage(CWizardPage* pPage);
	void SetActivePageByID(UINT nResourceID);

	void SetTitle(LPCTSTR lpszText);
	void SetBackText(LPCTSTR lpszText);
	void SetNextText(LPCTSTR lpszText);
	void SetCancelText(LPCTSTR lpszText);
	void SetFinishText(LPCTSTR lpszText);

	void DoCancel( void );
	void DoFinish( void );

	virtual void LocalizeControls(void);



protected:
	BOOL ActivatePage(CWizardPage* pPage);
	BOOL DeactivatePage();
	void SetPlaceholderID(int nPlaceholderID);


private:
	void Init();
	BOOL DestroyPage(CWizardPage* pPage);
	CWizardPage* GetFirstPage();
	CWizardPage* GetLastPage();
	CWizardPage* GetActivePage() const;
	CWizardPage* GetNextPage();



protected:
	virtual void DoDataExchange(CDataExchange* pDX); 
	virtual BOOL OnInitDialog();
protected:

	// Generated message map functions
	//{{AFX_MSG(CWizardDialog)
	afx_msg void OnDestroy();
	afx_msg void OnPaint();
	afx_msg void OnWizardFinish();
	afx_msg void OnWizardBack(); 
	afx_msg void OnWizardNext(); 
	afx_msg void OnCancel(); 
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};


// *****************************************************************
class CWizardPage : public CDialog
{
	friend class CWizardDialog;

	// Construction
public:
	CWizardPage(CString backgroundfile, CWnd* pParent = NULL);   // standard constructor

	CWizardPage(CString backgroundfile, LPCTSTR lpszTemplateName, CWnd* pParent = NULL);
	CWizardPage(CString backgroundfile, UINT nIDTemplate, CWnd* pParent = NULL);
	virtual ~CWizardPage();


protected:
	CWizardDialog *m_pParent; // Parent dialog

private:
	BOOL m_bCreated; // flag to tell us if the dialog has been created
	BOOL m_bActive;		// flag to tell is if the dialog is the active page
	UINT m_nDialogID; // resource ID for thie page
	CString backgroundFile;

	// Operations
public:
	virtual BOOL OnCreatePage();
	virtual void OnDestroyPage();

	virtual void LocalizeControls(void);

	// these functions are the same as CPropertyPage
	virtual void OnCancel();
	virtual BOOL OnKillActive();
	virtual void OnSetActive();
	virtual BOOL OnQueryCancel( );
	virtual LRESULT OnWizardBack();
	virtual LRESULT OnWizardNext();
	virtual BOOL OnWizardFinish();

    // Progress Indikation
	virtual void ProcessFeedback( void );
	virtual void ReportProgress(CString text, long current, long end);


	// Utility Fuctions
	unsigned long long DownloadFile(CString url, CString destPath, CString progressMessage );



	// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CWizardPage)
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

	// Implementation
protected:


	HBITMAP backgroundLocalized;
	COLORREF backgroundLocalizedTransparent;

	HBITMAP background;		
	COLORREF backgroundTransparent;


	// Generated message map functions
	//{{AFX_MSG(CWizardPage)
	virtual BOOL OnInitDialog();
	afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
	afx_msg void OnPaint(void);
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};




#pragma endregion

#pragma region SetupApp
DECLARE_HANDLE(HZIP);

class CSetupApp : public CWinApp
{
	bool (* initializeFunction)(void);
	HZIP zipResource;
	CString languagePrefix;
	CString neutralLanguagePrefix;

public:
	static volatile bool CancelSetup;
	static CSetupApp * SetupApp;
	static CWizardDialog * SetupDlg;
	CStringArray Messages; 
	
	// Resource management via embedded zip resource
	void AttachZipResource(int resourceID ); 
	bool ExistsResource( CString file );

	HBITMAP GetBitmapResource( CString file );
	HBITMAP GetLocalizedBitmapResource( CString file );

	CString GetTextResource(CString file );
	CString GetLocalizedTextResource(CString file );

	// Multi Langauage Support
	void SetLanguagePrefix(CString prefix);
	void SetNeutralLanguagePrefix(CString prefix);

	inline CString GetLanguagePrefix( void ) {return languagePrefix; }
	inline CString GetNeutralLanguagePrefix( void ) {return neutralLanguagePrefix; }




	

	// .NET Detection
	bool CheckNetfx30BuildNumber();	// Validate .NET 3.0 build number 
	bool CheckNetfx35BuildNumber(); // Validate .NET 3.5 build number 
	int GetNetfx10SPLevel();        // Get service pack level for .Net 1.0
	int GetNetfx11SPLevel();        // Get service pack level for .Net 1.1
	int GetNetfx20SPLevel();        // Get service pack level for .Net 2.0
	int GetNetfx30SPLevel();        // Get service pack level for .Net 3.0
	int GetNetfx35SPLevel();        // Get service pack level for .Net 3.5
	bool IsNetfx10Installed();      // Check for .Net 1.0
	bool IsNetfx11Installed();      // Check for .Net 1.1
	bool IsNetfx20Installed();      // Check for .Net 2.0
	bool IsNetfx30Installed();      // Check for .Net 3.0
	bool IsNetfx35Installed();      // Check for .Net 3.5

	// SQL Server Express Detection
	bool IsSQLExpress2005SP2Installed(); 


	public:
	CSetupApp( bool (* initializeFunction)(void) );
	virtual BOOL InitInstance();
	virtual int ExitInstance( void );

	DECLARE_MESSAGE_MAP()
};





#pragma endregion



